package co.fitstart.weapp.api.web.controller.profile;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.OfflineAgent;
import co.fitstart.weapp.api.service.StudentService;
import co.fitstart.weapp.api.service.UserAgentService;
import co.fitstart.weapp.api.service.UserService;

/**
 * 
 * @author dan
 *
 */
@RestController
@RequestMapping(value = "/profile-offline-agent")
public class ProfileOfflineAgentController {
    
    @Autowired
    private UserAgentService userAgentService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private StudentService studentService;
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "unionid")
    public HttpResult index(@RequestParam("unionid") String unionid) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            Student student = studentService.load(user);
            
            List<OfflineAgent> offlineAgents = userAgentService.listOfflineAgent(student);
            
            int courseAmount = 0;
            for(OfflineAgent offlineAgent : offlineAgents) {
                courseAmount = courseAmount + offlineAgent.getCourseAmount();
            }
            
            httpResult.success().message("加载数据成功")
                                .data("student", student)
                                .data("courseAmount", courseAmount)
                                .data("total", offlineAgents.size())
                                .data("offlineAgents", offlineAgents);
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }

}
